import axios from "axios"
import Cookies from 'js-cookie'
import Setting from '@/Setting'
const servece = axios.create({
    baseURL: Setting.apiBaseURL,
    timeout: 1000000
});

// axios.defaults.withCredentials = true
// axios.defaults.headers.post['Content-Type'] = 'application/json';

servece.interceptors.request.use(
    config => {
        let token = Cookies.get('token');
        if (token) {
            config.headers['Authori-zation'] = token;
        }
        return config;
    },
    error => {
        return Promise.reject(error)
    }
);

servece.interceptors.response.use(
    (res) => {
        let errorCodes = [400, 401, 403, 404, 422, 500];
        let data = res.data
        if (errorCodes.indexOf(data.status) !== -1) {
            return Promise.reject(data.msg);
        }

        return data;
    },
    error => {
        return Promise.reject(error)
    }
);

export default servece